package light.head.dao.el;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.el.ExamRecord;
@Service
public class ExamRecordDAO extends BaseDAO<ExamRecord, Integer>{
	private final static String hql_findByClcAndUc = "FROM ExamRecord er WHERE er.clc = ? AND er.cu = ? AND er.cc = ? AND er.deleted = 0";
	private final static String hql_findBestScoreInfo = "FROM ExamRecord er WHERE er.clc = ? AND er.cu = ? AND er.cc = ? AND er.deleted = 0 AND er.examScore = (SELECT MAX(er1.examScore) FROM ExamRecord er1 WHERE er1.clc = ? AND er1.cu = ? AND er1.cc = ? AND er1.deleted = 0)";
	private final static String hql_findByClcUc = "FROM ExamRecord er WHERE er.clc = ? AND er.cu = ? AND er.cc = ? AND er.deleted = 0 AND er.isMark = 1 AND markCu is null ORDER BY er.ct DESC";
	private final static String hql_findByClcUc1 = "FROM ExamRecord er WHERE er.clc = ? AND er.cu = ? AND er.cc = ? AND er.deleted = 0 AND er.isPass = 1 ORDER BY er.ct DESC";

	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pageBean) {
		if(oc == null) return null;
		StringBuffer sql = new StringBuffer("FROM ExamRecord ex WHERE ex.oc = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(oc);
		if(map != null){
			String clc = map.get("clc");
			String cc = map.get("cc");
			String cu = map.get(Parm.CU);
			//String ct = map.get(Parm.CT);
			String deleted = map.get(Parm.DELETED);
			
			if(isNotNull(clc)){
				sql.append(" AND ex.clc = ?");
				list.add(Integer.parseInt(clc));
			}
			if(isNotNull(cc)){
				sql.append(" AND ex.cc = ?");
				list.add(Integer.parseInt(cc));
			}
			if(isNotNull(cu)){
				sql.append(" AND ex.cu = ?");
				list.add(Integer.parseInt(cu));
			}
			if(isNotNull(deleted)){
				sql.append(" AND ex.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		sql.append(" ORDER BY ex.code DESC");
		return findToPageBean(sql.toString(), pageBean, list);
	}
	
	
	public List<ExamRecord>  findByClcAndUcAndCc(Integer clc,Integer uc,Integer cc){
		return findI(hql_findByClcAndUc,clc,uc,cc);
	}
	//判断是否有未阅卷的
	public List<ExamRecord>  findByClcUcCc(Integer clc,Integer uc,Integer cc){
		return findI(hql_findByClcUc,clc,uc,cc);
	}
	//判断是否有通过的
	public List<ExamRecord>  findByClcUcCc1(Integer clc,Integer uc,Integer cc){
		return findI(hql_findByClcUc1,clc,uc,cc);
	}
	public ExamRecord findBestScoreInfo(Integer clc,Integer uc,Integer cc){
		List<ExamRecord> list = findI(hql_findBestScoreInfo,clc,uc,cc,clc,uc,cc);
		if(list.size() > 0 && list != null){
			return list.get(0);
		}
		return null;
	}
	/**
	 * 更新
	 */
	public RESULT update(ExamRecord er){
		if(er == null || er.getCode() == null){
			return RESULT.ERROR;
		}
		ExamRecord old = get(er.getCode());
		if(er.getDeleted() != null && er.getDeleted() != old.getDeleted()){
			old.setDeleted(er.getDeleted());
		}
		if(er.getIsMark() != null && er.getIsMark() != old.getIsMark()){
			old.setIsMark(er.getIsMark());
		}
		if(er.getCc() != null && er.getCc() != old.getCc()){
			old.setCc(er.getCc());
		}
		if(er.getClc() != null && er.getClc() != old.getClc()){
			old.setClc(er.getClc());
		}
		if(er.getCu() != null && er.getCu() != old.getCu()){
			old.setCu(old.getCu());
		}
		if(er.getExamScore() != null && er.getExamScore().floatValue() != old.getExamScore().floatValue()){
			old.setExamScore(er.getExamScore());
		}
		if(er.getMarkCt() != null && er.getMarkCt() != old.getMarkCt()){
			old.setMarkCt(er.getMarkCt());
		}
		if(er.getMarkCu() != null && er.getMarkCu() != old.getMarkCu()){
			old.setMarkCu(er.getMarkCu());
		}
		if(er.getOc() != null && er.getOc() != old.getOc()){
			old.setOc(er.getOc());
		}
		return upd(old);
	}
	
	public List<ExamRecord> findClc(Integer oc) {
		StringBuffer sql = new StringBuffer("select distinct(er.clc),cl.name from examrecord er join Classes cl ON cl.code = er.clc WHERE er.oc = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(oc);
		
		List<ExamRecord> result = new ArrayList<ExamRecord>();
		List<Object> list1 = super.query(sql.toString(),null,null,list.toArray());
		
		ExamRecord er = null;
		Object[] obj;
		if (null != list1 && list1.size() > 0) {
			for (Object o : list1) {
				er = new ExamRecord();
				obj = (Object[]) o;
				er.setClc(obj[0] == null ? null : (Integer) obj[0]); 
				er.setClcn(obj[1] == null ? null : (String) obj[1]);
				result.add(er);
			}
		}
		return result;
	}
	
	public List<ExamRecord> findCc(Integer oc) {
		StringBuffer sql = new StringBuffer("select distinct(er.cc),c.name from examrecord er join Course c on c.code = er.cc WHERE er.oc = ? ");
		List<Object> list = new ArrayList<Object>();
		list.add(oc);
		List<ExamRecord> result = new ArrayList<ExamRecord>();
		List<Object> list1 = super.query(sql.toString(),null,null,list.toArray());
		
		ExamRecord er = null;
		Object[] obj;
		if (null != list1 && list1.size() > 0) {
			for (Object o : list1) {
				er = new ExamRecord();
				obj = (Object[]) o;
				er.setCc(obj[0] == null ? null : (Integer) obj[0]); 
				er.setCcn(obj[1] == null ? null : (String) obj[1]);
				result.add(er);
			}
		}
		return result;
	}
	
	public List<ExamRecord> findCu(Integer oc) {
		StringBuffer sql = new StringBuffer("select distinct(er.cu),u.realName from examrecord er join UserInfo u ON u.code = er.cu WHERE er.oc = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(oc);
		List<ExamRecord> result = new ArrayList<ExamRecord>();
		List<Object> list1 = super.query(sql.toString(),null,null,list.toArray());
		
		ExamRecord er = null;
		Object[] obj;
		if (null != list1 && list1.size() > 0) {
			for (Object o : list1) {
				er = new ExamRecord();
				obj = (Object[]) o;
				er.setCu(obj[0] == null ? null : (Integer) obj[0]); 
				er.setCn(obj[1] == null ? null : (String) obj[1]);
				result.add(er);
			}
		}
		return result;
	}
	
	public List<ExamRecord> findByClcAndCc(Integer clc,Integer cc){
		String hql = "FROM ExamRecord er WHERE er.deleted = 0 AND er.clc = ? AND er.cc = ?";
		return findI(hql, clc,cc);
	}
	public List<ExamRecord> findByPass(Integer clc,Integer cc){
		String hql = "FROM ExamRecord er WHERE er.deleted = 0 AND er.isPass = 1 AND er.clc = ? AND er.cc = ?";
		return findI(hql, clc,cc);
	}
}
